﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.hyrentals.dbaccess;
using com.hyrentals.data;
using com.hyrentals.common;

namespace com.hyrentals.logic
{
    public class RentalRequestLogic
    {
        private RentalRequestDBAccess _rentalRequestDBAccess;

        public RentalRequestLogic()
        {
            _rentalRequestDBAccess = new RentalRequestDBAccess();
        }

        public bool InsertRentalRequest(RV rv, string renterId, DateTime checkinTime, DateTime checkoutTime)
        {
            RVPricing pricing = new RVPricing()
            {
                RentalRateDaily = rv.Pricing.RentalRateDaily,
                RentalRateWeekly = rv.Pricing.RentalRateWeekly,
                RentalRateMonthly = rv.Pricing.RentalRateMonthly,
                MilageFreeMilesDaily = rv.Pricing.MilageFreeMilesDaily,
                MilageFreeMilesWeekly = rv.Pricing.MilageFreeMilesWeekly,
                MilageFreeMilesMonthly = rv.Pricing.MilageFreeMilesMonthly,
                MilagePerExtraMileDaily = rv.Pricing.MilagePerExtraMileDaily,
                MilagePerExtraMileWeekly = rv.Pricing.MilagePerExtraMileWeekly,
                MilagePerExtraMileMonthly = rv.Pricing.MilagePerExtraMileMonthly,
                DepositNonrefundableSecurityDownPayment = rv.Pricing.DepositNonrefundableSecurityDownPayment,
                DepositRefundableDamageProtection = rv.Pricing.DepositRefundableDamageProtection,
                DepositRefundableCleaningCharge = rv.Pricing.DepositRefundableCleaningCharge,
                LateChargePerHour = rv.Pricing.LateChargePerHour,
                OtherCharges = rv.Pricing.OtherCharges
            };

            RentalRequest rentalRequest = new RentalRequest()
            {
                Id = GUIDHelper.Generate32GUID(),
                RenterId = renterId,
                RVId = rv.Id,
                State = RentalRequestStateType.Created_Pending_Owner_Accept,
                OpenTime = DateTime.UtcNow,
                CheckInTime = checkinTime,
                CheckOutTime = checkoutTime,
                Pricing = pricing
            };

            return _rentalRequestDBAccess.InsertRentalRequest(rentalRequest);
        }

        public IEnumerable<RentalRequest> GetReservationsByUserId(string UserId)
        {
            return _rentalRequestDBAccess.GetRentalRequestsByRVOwnerId_Paging(UserId, 1, 10);
        }

        public IEnumerable<RentalRequest> GetTripsByUserId(string UserId)
        {
            return _rentalRequestDBAccess.GetRentalRequestsByRenterId_Paging(UserId, 1, 10);
        }
    }
}
